package com.linkage.educloud.ah.mvp.model;

import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.bean.ClazzGroup;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.mvp.CommonModel;
import com.linkage.educloud.ah.mvp.contract.IContactContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * <desc>
 * Created by The Moss on 2018/11/8.
 */

public class ContactModel extends CommonModel implements IContactContract.IModel {
    @Override
    public Observable loadDatasFromLocal() {
        return Observable.create(new ObservableOnSubscribe<List<ClazzGroup>>() {
            @Override
            public void subscribe(ObservableEmitter<List<ClazzGroup>> e) {
                try {
                    List<ClazzGroup> groups = MyApplication.getInstance().getResovler().getGroups(AccountConfig.getLoginAccount().getLoginAccount());
                    if (groups == null) {
                        e.onError(new Throwable("no groups data"));
                    } else {
                        e.onNext(groups);
                    }
                    e.onComplete();
                } catch (Exception e1) {
                    e1.printStackTrace();
                    e.onError(new Throwable(e1.getMessage()));
                }
            }
        });
    }
}
